﻿using System.Xml.Linq;

namespace SchemaGen.Xsd
{
    public class SSimpleElement : SEntityBase
    {
        public SSimpleType Restriction { get; set; }
        public int MinOccurs { get; set; }
        public int MaxOccurs { get; set; }

        public SSimpleElement(string name)
            : base(SNames.Elements.Element, name)
        {
            MinOccurs = 1;
            MaxOccurs = 1;
        }

        internal override XElement Create()
        {
            var result = base.Create();

            if (MinOccurs >= 0 && MinOccurs != 1)
                result.Add(new XAttribute("minOccurs", MinOccurs));

            if (MaxOccurs != 1)
                result.Add(new XAttribute("maxOccurs", MaxOccurs == int.MaxValue || MaxOccurs < 0 ? "unbounded" : MaxOccurs.ToString()));

            if (Restriction != null) result.Add(Restriction.Create());

            return result;
        }
    }
}
